<?php
class Cms_CourseController extends Zend_Controller_Action {
    private $_languages = array();
    private $_course = array();
    private $_page = 10;
    public function init() {
        $this->_languages = Utils_Global::getSupportLanguages();
        
        $lang = Utils_Global::$params['lang'];
        if(!in_array($lang, $this->_languages)) {
        	$lang = Utils_Global::getCurrentLanguage();
        }
        if(!$this->_course) {
            $id = Utils_Global::$params['id'];
            $model = Cms_Model_Course::factory();
            $course = $model->getCourses(array('id' => $id, 'lang' => $lang, 'include_seo' => 1));
            $this->_course = $course['0'];
        }
    }
    
	public function detailAction() {
	    $id = Utils_Global::$params['id'];
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    $model = Cms_Model_Course::factory();
	    $tabs = $model->getTabs($id, $lang);
	    $subCourses = $model->getCourses(array('parent' => $id, 'lang' => $lang,));
	    $this->view->course = $this->_course;
	    $this->view->tabs = $tabs;
	    $this->view->subcourses = $subCourses;
	    $this->view->lang = $lang;
	    $breadcrumbData = array(
	    		array('url' => $this->view->serverUrl(),
	    				'displayName' => $this->view->translate('home', $lang),
	    				'title' => $this->view->translate('home', $lang),
	    		),
	    		array(
	    				'url' => 'javascript:void(0)',
	    				'displayName' => $this->view->translate('course', $lang),
	    				'title' => $this->view->translate('course', $lang),
	    		),
	    		array(
	    				'url'=> 'javascript:void(0)',
	    				'displayName' => $this->_course['content'],
	    				'title' => $this->_course['content'],
	    		)
	    );
	     
	    $this->view->breadcrumb = $breadcrumbData;
	    $metaInfo = array('title' => $this->_course['seo_title_content'],
	                        'description' => $this->_course['seo_description_content'],
	                        'keyword' => $this->_course['seo_keyword_content'],
	    );
	    $this->view->metadata = $this->view->metadata('', $metaInfo,
	    		$this->view->serverUrl() . $this->view->url(array()));
	}
	/**
	 *@deprecated 
	 * */
	public function breadcrumbAction() {
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    $breadcrumbData = array(
	            array('url' => $this->view->serverUrl(),
	                    'displayName' => $this->view->translate('home', $lang),
	                    'title' => $this->view->translate('home', $lang),
        	    ),
	    		array(
	    				'url' => 'javascript:void(0)',
	    				'displayName' => $this->view->translate('course', $lang),
	    				'title' => $this->view->translate('course', $lang),
	    		),
	    		array(
	    				'url'=> 'javascript:void(0)',
	    				'displayName' => $this->_course['content'],
	    				'title' => $this->_course['content'],
	    		)
	    );
	    
	    $this->view->data = $breadcrumbData;
	}
	
	public function registerFullAction() {
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    
	    $model = Cms_Model_Page::factory();
	    $options = array('id' => $this->_page, 'lang' => $lang);
	    $page = $model->select($options);
	    if($page) {
	    	$page = $page[0];
	    }
	    
	    $model = Cms_Model_Course::factory();
	    $courses = $model->getCoursesWithSub(array('lang' => $lang));
	    $this->view->lang = $lang;
	    $this->view->courses = $courses;
	    $this->view->page = $page;
	    Utils_Global::$params['registercourseclzz'] = 'current';
	    $breadcrumbData = array(
    		array('url' => $this->view->serverUrl(),
    				'displayName' => $this->view->translate('home', $lang),
    				'title' => $this->view->translate('home', $lang),
    		),
    		array(
    				'url' => 'javascript:void(0)',
    				'displayName' => $this->view->translate('register_breadcrumb', $lang),
    				'title' => $this->view->translate('register_breadcrumb', $lang),
    		),
	    );
	    
	    $this->view->breadcrumb = $breadcrumbData;
	    $this->view->metadata = $this->view->metadata('course_registration', array('title' => $this->view->translate('seo_register_course_title', $lang),
	                                                                                'description' => $this->view->translate('seo_register_course_description', $lang),
	    ),
	    		$this->view->serverUrl() . $this->view->url(array()));
	}
	
	public function registerAction() {
	    $fullname = trim(Utils_Global::$params['fullname']);
	    $email = trim(Utils_Global::$params['email']);
	    $phone = trim(Utils_Global::$params['phone']);
	    $location = trim(Utils_Global::$params['location']);
	    $course = Utils_Global::$params['course'];
	    $parentCourse = Utils_Global::$params['parentcourse'];
	    $attenderCount = Utils_Global::$params['count'];
	    $address = Utils_Global::$params['address'];
	    $company = Utils_Global::$params['company'];
	    $title = Utils_Global::$params['title'];
	    $payMethod = Utils_Global::$params['paymentmethod'];
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    $result = array();
	    if(!$fullname) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng nhập họ tên';
	        $this->_helper->json($result);
	    }
	    if(!$email || !Utils_CommonFunction::isValidEmail($email)) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng nhập email hợp lệ';
	        $this->_helper->json($result);
	    }
	    if(!$phone || !Utils_CommonFunction::isValidPhone($phone)) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng nhập số điện thoại hợp lệ';
	        $this->_helper->json($result);
	    }
	    if(!$location) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng chọn địa điểm học';
	        $this->_helper->json($result);
	    }
	    if(!$course) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng chọn khóa học';
	        $this->_helper->json($result);
	    }
	    //Render email 
	    $this->view->fullname = $fullname;
	    $this->view->email = $email;
	    $this->view->phone = $phone;
	    $this->view->location = $location;
	    $this->view->address = $address;
	    $this->view->company = $company;
	    $this->view->title = $title;
	    $this->view->count = $attenderCount;
	    $this->view->paymentMethod = $payMethod;
	    $model = Cms_Model_Course::factory();
	    $options = array('id' => $course, 'lang' => $lang);
	    if($course != $parentCourse) {
	        $options['parent'] = $parentCourse;
	    }
	    $course = $model->getCourses($options);
	    $this->view->course = $course[0];
	    $emailContent = $this->view->render('course/register.phtml');
	    //send email to fmit
	    $fmitEmail = Utils_Global::getConfig('cms', 'application', 'fmitemail');
	    $fmitEmail = $fmitEmail->toArray();
	    $sent = Utils_CommonFunction::sendMail($fmitEmail, 'Đăng ký học', $emailContent);
	    if($sent) {
	        $result['code'] = 1;
	        $result['message'] = $this->view->translate('success_registration', $lang);
	        $this->_helper->json($result);
	    } else {
	        $result['code'] = 0;
	        $result['message'] = "Có lỗi xảy ra, vui lòng thử lại!";
	        $this->_helper->json($result);
	    }
	}
}
